@extends('layouts.back_stage')
@section('content')
    <style>
        .form-control{font-size:13px;}
        .price{border:1px solid #ccc;width:100px;}
        .cur{cursor:pointer;}
        .handle_a{margin-right:10px;}
        .handle_sure{margin-right:10px;}
        .ind{text-indent:30px;}
        .year,.season,.brand,.size{width: 130px;display: inline-block;margin-right:15px;font-size:13px;}
    </style>
    <div class="container">
        <div style="padding:20px 20px 20px 0;overflow:hidden;">
            <h3 style="float:left;">生产成本</h3>
        </div>
        <form action="/admin/operation/product_cost_lists" role="form">
            <label>品牌：</label>
                <select class="brand form-control" name="brand">
                    <option value="">请选择</option>
                    @foreach($brands as $k => $v)
                        <option value="{{$k}}"
                            @if($whereInfo->brand==$k)
                                selected
                            @endif
                        >{{$v}}</option>
                    @endforeach
                </select>
            <label>款号：</label><input type="text"  placeholder="请填写款号" class="form-control size" value="{{$whereInfo->girard}}" name="girard">
            <label>年份：</label>
                <select class="year form-control" name="year">
                    @if(!empty($whereInfo->year))
                        <option value="{{$whereInfo->year}}">{{$whereInfo->year}}</option>
                    @endif
                    <option value="">请选择</option>
                    <option value="2016">2016</option>
                    <option value="2017">2017</option>
                    <option value="2018">2018</option>
                    <option value="2019">2019</option>
                </select>
            <label>季节：</label>
            <select class="season form-control" name="quarter">
                @if(!empty($whereInfo->quarter))
                    <option value="{{$whereInfo->quarter}}">{{$whereInfo->quarter}}</option>
                @endif
                <option value="">请选择</option>
                <option value="春季">春季</option>
                <option value="夏季">夏季</option>
                <option value="秋季">秋季</option>
                <option value="冬季">冬季</option>
            </select>
            <button type="submit" class="btn btn-info" id="sort_btn" style="margin-bottom:4px;">搜索</button>
        </form>
        <table class="table table-hover" style="margin-top: 20px;">
            <thead>
            <tr>
                <th>品牌</th>
                <th>品名</th>
                <th>款号</th>
                <th>年份</th>
                <th>季节</th>
                <th>大类</th>
                <th>子类</th>
                <th>波段</th>
                <th>零售价</th>
                <th>工艺加工费</th>
                <th class="col-md-1">原料成本</th>
                <th class="col-md-1">管理成本</th>
                <th class="col-md-1">整件加工费</th>
                <th class="ind">操作</th>
            </tr>
            </thead>
            <tbody>
                @foreach($pro_infos as $k => $v)
                <tr class="tr_{{$v->procost_details_id}} tr_lis" abc="{{$v->procost_details_id}}">
                    <td class="cur">{{$brands[$v->brand]}}</td>
                    <td class="cur">{{$v->name}}</td>
                    <td class="cur">{{$v->girard}}</td>
                    <td class="cur">{{$v->year}}</td>
                    <td class="cur">{{$v->quarter}}</td>
                    <td class="cur">{{$cate_infos[$v->cate1]}}</td>
                    <td class="cur">{{$cate_infos[$v->cate2]}}</td>
                    <td class="cur">{{$v->band}}</td>
                    <td class="cur">{{$v->retail_price}}</td>
                    <td class="cur">{{!is_null($v->processing_fee)?$v->processing_fee:0}}</td>
                    <td class="material"><input type="text" class="material_cost price" value="{{$v->material_costs}}" disabled="disabled" maxlength='10' onkeyup="clearNoNum(this)"></td>
                    <td class="administration"><input type="text" class="administration_cost price" value="{{$v->manager_cost}}" disabled="disabled" maxlength='10' onkeyup="clearNoNum(this)"></td>
                    <td class="total"><input type="text" class="total_cost price" value="{{$v->all_fee}}" disabled="disabled" maxlength='10' onkeyup="clearNoNum(this)"></td>
                    <td class="handle">
                        <a class="handle_a" href="javascript:;">成本录入</a>
                        <a href="/storage/product_cost_details?id={{$v->procost_details_id}}">工艺费录入</a>
                    </td>
                </tr>
               @endforeach
            </tbody>
        </table>
        <div style="float: right"><div class="return_box" style="float: right;margin:20px 0;"><button class="btn btn-info" onclick="returnPage()" style="float:right;">跳转</button><input type="text" class="form-control returnPage" style="width:6rem;float: right;margin:0 5px;"></div>
            {{$pro_infos->appends(['brand'=>$whereInfo->brand,'year'=>$whereInfo->year,'quarter'=>$whereInfo->quarter,'girard'=>$whereInfo->girard])->links()}}
        </div>
        <div class="ShowDiv" style="display:none;">
            <table class="table table-hover" style="margin-top: 80px;width:30%;">
                <tr><th>工艺名称</th><th>价格</th></tr>

            </table>
        </div>
    </div>
    <script>
        /*价格*/
        function clearNoNum(obj){
            obj.value = obj.value.replace(/[^\d.]/g,"");
            obj.value = obj.value.replace(/\.{2,}/g,".");
            obj.value = obj.value.replace(/^\./g,"");
            obj.value = obj.value.replace(".","$#$").replace(/\./g,"").replace("$#$",".");
            obj.value = obj.value.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3');
            if(obj.value.indexOf(".")< 0 && obj.value !=""){
                obj.value= parseFloat(obj.value);
            }
        }
        /*价格录入*/
        $(document).on("click",".handle_a",function(){
            var id=$(this).parent(".handle").parent().attr("abc");
            $(".tr_"+id).children(".handle").children(".handle_a").remove();
            $(".tr_"+id).children(".handle").prepend("<a class='handle_sure' href='javascript:;'>确定</a>");
            $(".tr_"+id).children().children(".price").removeAttr("disabled","disabled");
        });
        /*确定*/
        $(document).on("click",".handle_sure",function(){
            var id=$(this).parent(".handle").parent().attr("abc");
            var total=$(".tr_"+id).children(".total").children(".total_cost").val();
            var administration=$(".tr_"+id).children(".administration").children(".administration_cost").val();
            var material=$(".tr_"+id).children(".material").children(".material_cost").val();
            if( Number(material) < 0 || material==''){
                $(".faceImg").attr("src","/img/weep.png");
                $(".oHandle").text("原料成本不能小于0元或者为空");
                $(".oSuccess").show(0).delay(1000).hide(0);
                return false;
            }
            if( Number(administration) < 0 || administration==''){
                $(".faceImg").attr("src","/img/weep.png");
                $(".oHandle").text("管理成本不能小于0元或者为空");
                $(".oSuccess").show(0).delay(1000).hide(0);
                return false;
            }
            if( Number(total) < 0 || total==''){
                $(".faceImg").attr("src","/img/weep.png");
                $(".oHandle").text("整件加工费不能小于0元或者为空");
                $(".oSuccess").show(0).delay(1000).hide(0);
                return false;
            }
            $.ajax({
                headers: {'X-CSRF-TOKEN': $('meta[name="csrf_token"]').attr('content')},
                url:'/storage/cost_details_save',
                data:{
                    'procost_details_id':id,
                    'material_costs':material,
                    'manager_cost':administration,
                    'all_fee':total
                },
                type:'post',
                dataType: 'json',
                success:function (data) {
                    if(data.success) {
                        $(".faceImg").attr("src","/img/laugh.png");
                        $(".oHandle").text("录入成功");
                        $(".oSuccess").show(0).delay(1000).hide(0);
                        $(".tr_"+id).children(".handle").children(".handle_sure").remove();
                        $(".tr_"+id).children(".handle").prepend("<a class='handle_a' href='javascript:;'>成本录入</a>");
                        $(".tr_"+id).children().children(".price").attr("disabled","disabled");
                    }else {
                        $(".faceImg").attr("src","/img/weep.png");
                        $(".oHandle").text(data.msg);
                        $(".oSuccess").show(0).delay(1000).hide(0);
                    }
                }
            })
        });
        /*点击*/
        var flag=true;
        $(document).on("click",".cur",function(){
            $(".tr_name").remove();
            $(".tr_num").remove();
            var id=$(this).parent().attr("abc");
            if(flag){
                $.ajax({
                    headers: {'X-CSRF-TOKEN': $('meta[name="csrf_token"]').attr('content')},
                    type: 'post',
                    url: '/admin/operation/cost_list',
                    data: {'pcost_details_id': id},
                    dataType: 'json',
                    beforeSend:function(){//触发ajax请求开始时执行
                        flag=false;
                    },
                    success: function (data) {
                        var str='';
                        console.log(data);
                        if(data.success){
                            $(".ShowDiv").show();
                            for(var i in data.info){
                                str+='<tr class="tr_name"><td>'+i+'</td><td>'+data.info[i]+'</td></tr>';
                            }
                            $(".ShowDiv").children(".table").append(str);
                        }else{
                            $(".ShowDiv").hide();
                            $(".faceImg").attr("src","/img/weep.png");
                            $(".oHandle").text(data.msg);
                            $(".oSuccess").show(0).delay(1000).hide(0);
                        }
                    },
                    complete:function(){//ajax请求完成时执行
                        flag=true;
                    }
                });
            }
        });
        $(document).ready(function () {
            if($('.tr_lis').length < 10){
                $('.return_box').hide()
            }else{
                $('.return_box').show()
            }
        })
    </script>
@endsection